#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = l; i <= r; ++i)
using namespace std;
const int N = 10005;
int fa[N];
int Find(int x) {
    if (x != fa[x]) fa[x] = Find(fa[x]);
    return fa[x];
}
void merge(int x, int y) {
    x = Find(x);
    y = Find(y);
    if (x != y) fa[x] = y;
}
int main() {
    rep(i, 1, 10000) fa[i] = i;
    int n, k, u, v;
    set<int> vis;
    cin >> n;
    rep(_, 1, n) {
        cin >> k;
        if (k) {
            cin >> u;
            vis.insert(u);
        }
        rep(i, 2, k) {
            cin >> v;
            vis.insert(v);
            merge(u, v);
        }
    }
    int num = 0;
    for (int i : vis) num += i == Find(i);
    cout << num << ' ' << vis.size() << '\n';
    cin >> n;
    rep(_, 1, n) {
        cin >> u >> v;
        if (Find(u) == Find(v))
            cout << "Yes\n";
        else
            cout << "No\n";
    }
    return 0;
}